import {GameObjects} from "./GameObjects.js";

import {Wall} from "./Wall.js"
import {Snake} from "./Snake.js";


export class GameMap extends GameObjects{
    constructor(ctx,parent) {
        super();
        this.ctx=ctx;
        this.parent=parent;
        this.L=0;
        this.rows=17;
        this.cols=17;
        this.walls=[];
        this.inner_Walls_count=15;

        this.snakes=[
            new Snake({id:0,color:"#4876EC",r:this.rows-2,c:1},this),
            new Snake({id:1,color:"#F94848",r:1,c:this.cols-2},this),
        ];


    }

    add_listening_events(){
        this.ctx.canvas.focus();
        const [snake0,snake1]=this.snakes;
        //聚焦
        this.ctx.canvas.addEventListener("keydown",e=>{
            if(e.key==="w"){
                console.log("w")
                e.preventDefault()
                snake0.set_direction(0);

            }
            else if(e.key==="d"){
                e.preventDefault()
                snake0.set_direction(1);

            }
            else if(e.key==="s"){
                e.preventDefault()
                snake0.set_direction(2);
            }
            else if(e.key==="a"){
                e.preventDefault()
                snake0.set_direction(3);
            }
            else if(e.key==="ArrowUp"){
                e.preventDefault()
                snake1.set_direction(0);
            }
            else if(e.key==="ArrowRight"){
                e.preventDefault()
                snake1.set_direction(1);
            }
            else if(e.key==="ArrowDown"){
                e.preventDefault()
                snake1.set_direction(2);
            }
            else if(e.key==="ArrowLeft"){
                e.preventDefault()
                snake1.set_direction(3);
            }
        })


    }

    start(){
       // this.create_Walls()
        for(let i=0;i<1000;i++)
        {
            if(this.create_Walls())
                break;
        }
        this.add_listening_events();

    }
    check_valid(cell){
        for(const wall of this.walls){
            if(wall.r===cell.r&&wall.c===cell.c){
                return false;
            }
        }
        for(const snake of this.snakes){
            let k=snake.cells.length;
            // k--;
            if(!snake.check_tail_licreasing()){
                k--;
            }
            for(let i=0;i<k;i++){
                if(snake.cells[i].r===cell.r&&snake.cells[i].c===cell.c){
                    return false;
                }
            }
        }
        return true;
        //不能撞到蛇蛇的身体
    }
    create_Walls(){
        // new Wall(0,0,this);
        const g=[];
        for(let r=0;r<this.rows;r++){
            g[r]=[];
            for(let c=0;c<this.cols;c++){
                g[r][c]=false;
            }
        }
        for(let r=0;r<this.rows;r++){
            //给四周添上障碍物
            g[r][0]=g[r][this.cols-1]=true;
        }
        for(let r=0;r<this.cols;r++){
            //给四周添上障碍物
            g[0][r]=g[this.rows-1][r]=true;
        }
        for(let i=0;i<this.inner_Walls_count;i++){
            for(let j=0;j<1000;j++){
                let r=parseInt(Math.random()*this.rows);
                let c=parseInt(Math.random()*this.cols);
                if(g[r][c]||g[this.rows-1-r][this.cols-1-c])continue;
                if(r===this.rows-2&&c===1||r===1&&c===this.cols-2)continue;
                g[r][c]=g[this.rows-1-r][this.cols-1-c]=true;
                break;
            }
        }

        const copy_g=JSON.parse(JSON.stringify(g))
        if(!this.check_connectivity(copy_g,1,this.rows-2,this.cols-2,1))return false;
        for(let r=0;r<this.rows;r++){
            for(let c=0;c<this.cols;c++){
                if(g[r][c]){
                    this.walls.push(new Wall(r,c,this));
                }
            }
        }

        return true;

    }
    check_connectivity(g,sx,sy,tx,ty){
        if(sx===tx&&sy===ty){
            return true;
        }
        g[sx][sy]=true;

        let dx=[-1,0,1,0];
        let dy=[0,1,0,-1];
        for(let i=0;i<4;i++){
            let x=dx[i]+sx;
            let y=dy[i]+sy;
            if(!g[x][y]&&this.check_connectivity(g,x,y,tx,ty))return true;

        }
        return false;
        // this.create_Walls();
        //要保证蛇能够通过

    }
    check_ready(){
        for(const snake of this.snakes){
            if(snake.status!=="idle"){
                return false;
            }
            if(snake.direction===-1)return false;

        }return true;
    }
    next_step(){
        for(const snake of this.snakes){
            snake.next_step();
        }
    }


    update_size(){
        this.L=Math.min(this.parent.clientWidth/this.rows,this.parent.clientHeight/this.cols);
        this.ctx.canvas.width=this.L*this.cols;
        this.ctx.canvas.height=this.L*this.rows;
    }

    update(){
        this.update_size()
        if(this.check_ready()){
            this.next_step();
        }
        this.render();

    }
    render(){
        const color_even="#AAD751"
        const color_odd="#a7ea71"
        for(let i=0;i<this.rows;i++){
            for(let j=0;j<this.cols;j++){
                if((i+j)%2==0){
                    this.ctx.fillStyle=color_even;

                }
                else{
                    this.ctx.fillStyle=color_odd;
                }
                this.ctx.fillRect(i*this.L,j*this.L,this.L,this.L);
            }
        }


    }

}